import re

import scrapy
import logging

from scrapy import Request

from ArticleSpider.items import McGysbItem

logger = logging.getLogger(__name__)

"""
中国制造网-工业设备
"""


class McGysbSpider(scrapy.Spider):
    name = 'mc_gysb'
    allowed_domains = ['cn.made-in-china.com', 'jixie.cn.made-in-china.com']
    start_urls = ['https://jixie.cn.made-in-china.com/qualitysuppliers/all-1.html']

    # 爬去入口
    def parse(self, response):
        page_num = response.meta.get("pageNum", 1)
        #
        # 企业列表
        company_list = response.xpath("//div[@class='company']/ul/li[@class='company-item']")
        for company in company_list:
            company_href = company.xpath("div/dl//dd/a/@href").extract()[0]
            company_url = company_href.replace("//", "https://")
            yield Request(url=company_url,
                          meta={"pageNum": page_num, "companyUrl": company_url},
                          callback=self.load_company_home)

        # 分页列表
        # https://jixie.cn.made-in-china.com/qualitysuppliers/all-1.html
        current_page = response.xpath("//div[@id='pager']/div[@class='pager-nav']/a[contains(@class, 'current')]")
        if current_page:
            next_page = current_page.xpath("following-sibling::a[1]")
            if next_page:
                next_page_num = next_page.xpath("text()").extract()[0]
                next_page_url = "http://jixie.cn.made-in-china.com/qualitysuppliers/all-{}.html".format(next_page_num)
                yield Request(url=next_page_url, meta={"pageNum": next_page_num}, callback=self.parse)

    # 企业信息
    def load_company_home(self, response):
        page_num = response.meta.get("pageNum", None)
        company_home_url = response.meta.get("companyUrl", None)

        # https://15957621642.cn.made-in-china.com
        # https://zhuchengbeier.cn.made-in-china.com
        # 公司信息
        # https://cn.made-in-china.com/showroom/zhuchengbeier-companyinfo.html
        # https://cn.made-in-china.com/showroom/15957621642-companyinfo.html
        # 联系信息
        # https://cn.made-in-china.com/showroom/zhuchengbeier-contact.html
        # https://cn.made-in-china.com/showroom/15957621642-contact.html

        match_re = re.match("https://(\\w+.*).cn.made-in-china.com", company_home_url)
        if match_re:
            company_id = match_re.group(1)
            company_info_url = "https://cn.made-in-china.com/showroom/{}-companyinfo.html".format(company_id)
            yield Request(url=company_info_url, meta={"pageNum": page_num, "companyInfoUrl": company_info_url}, callback=self.load_company_detail)

    # 企业信息
    def load_company_detail(self, response):
        page_num = response.meta.get("pageNum", None)
        company_info_url = response.meta.get("companyInfoUrl", None)
        logger.info("当前页：{}，公司地址：{}".format(page_num, company_info_url))
        #
        # 标题
        name = response.xpath("//ul[@class='contactInfo']/li[1]/strong/text()").extract()[0]
        # 电话
        # phone = response.xpath("//ul[@class='contactInfo']/li[2]/p/text()").extract()[0]
        # 传真
        cz = response.xpath("//ul[@class='contactInfo']/li[3]/span[2]/text()").extract()[0]
        # 地址
        address = response.xpath("//ul[@class='contactInfo']/li[4]/span[2]/text()").extract_first("")
        # 主营业务
        zyyw_selecter = response.xpath("//table[@class='memb-lst']/tbody/tr[1]/td/label")
        zyyw = ""
        for zyyw_selecter_item in zyyw_selecter:
            zyyw += zyyw_selecter_item.xpath("text()").extract_first("")
        # 业务范围
        ywfw = response.xpath("//table[@class='memb-lst']/tbody/tr[2]/td/text()").extract_first("")
        #
        item = McGysbItem()
        item['category'] = "mc_gysb"
        item['company_info_url'] = company_info_url
        item['name'] = name
        item['phone'] = ""
        item['cz'] = cz
        item['address'] = address
        item['zyyw'] = zyyw
        item['ywfw'] = ywfw
        yield item
